<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="i-ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="i-mdi:chevron-down" />
          </a-button>
        </a-dropdown>
      </template>
      <template #shortUrl="{ record, text }">
        <a :href="record.shortUrl" target="_blank">{{ record.code }}</a>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableActions(record)" />
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts" name="short-link-access-log" setup>
  import { ref, unref } from "vue"
  import { BasicTable, TableAction } from "/@/components/Table"
  import { listAccessLog, batchAccessLog, batchDeleteAccessLog } from "./api"
  import { columns, searchFormSchema } from "./data"
  import { useListPage } from "/@/hooks/system/useListPage"
  import { Icon } from "/@/components/Icon"
  import { useRouter } from "vue-router"
  const router = useRouter()
  let code = router.currentRoute.value.query?.code
  console.log("currentRoute.params: ", code)

  // 列表页面公共参数、方法
  const { tableContext } = useListPage({
    designScope: "linkAccessLog",
    tableProps: {
      alwaysShowTitle: true,
      title: "访问日志",
      api: listAccessLog,
      beforeFetch: (params) => {
        params.code = code || params.code
      },
      columns: columns,
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
      },
      bordered: false,
      actionColumn: {
        width: 90,
        title: "操作",
        dataIndex: "action",
        fixed: "right",
        slots: { customRender: "action" },
      },
    },
  })

  const [registerTable, { reload, getForm }, { rowSelection, selectedRowKeys }] = tableContext

  /**
   * 操作列定义
   * @param record
   */
  function getTableActions(record) {
    return [
      {
        label: "删除",
        color: "error",
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
    ]
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await batchAccessLog({ id: record.id }, reload)
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeleteAccessLog({ ids: selectedRowKeys.value }, reload)
  }
</script>
